package com.enterprise.web;

import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.assg2.dao.*;
import com.movie.Movie;
import com.movie.Room;
import com.movie.Show;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.Set;
import javax.servlet.http.HttpSession;

/**
 * This is the command that will be used for logging in users.
 * If logon is successful, the command should place a list of contacts
 * in the request attriubutes.
 * @author  yunki
 */
public class AddShowTimeCommand implements Command {

    /** Creates a new instance of LoginCommand */
    public AddShowTimeCommand() {
    }

    public String execute(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        //TODO: set the response content type to "text/html" here
        //TODO: You'll have to use getWriter() to output some HTML texts
        //TODO: close the output stream


        response.setContentType("text/html");// from response, set content type
        PrintWriter out = response.getWriter();// from response, get output writer

        HttpSession session = request.getSession(true);
           int idcinema = Integer.parseInt(request.getParameter("idcinema"));
           int idroom = Integer.parseInt(request.getParameter("room"+idcinema));
           String timeString = request.getParameter("time"+idcinema);
           
       //    SimpleDateFormat fomatter = new SimpleDateFormat("HH:mm:ss");
           
           Date time = TransformToTime(timeString);
           Movie  movie = (Movie)session.getAttribute("movieaddshow");
           DaoRoom dm = new DaoRoom();
           Room room = (Room)dm.findById(idroom);
           Set<Show> showSet = room.getShows();
           
           Iterator it = showSet.iterator();
           while(it.hasNext()){
               Show show = (Show)it.next();
               if(show.getStarts().equals(time));
                     request.setAttribute("message", "Time conflict occurs when add a showtime to this movie:<a href='javascript:history.go(-1)'>Go to Back</a>");
                    return "message";          
           }
           Show show = new Show(time, movie.getIdmovie(), idroom);
           DaoShow ds = new DaoShow();
           ds.save(show);
           request.setAttribute("message", "Sucessfully Add:<a href='javascript:history.go(-1)'>Go to Back</a>");
                    return "message";
           
                   
           
           
        
        
            

             
            }

  

    protected Date TransformToTime(String str_time) {
        try {
            DateFormat dfm = new SimpleDateFormat("HH:mm:ss");
            return dfm.parse(str_time);
        } catch (ParseException e) {
            System.out.println("Exception :" + e);
        }
        return null;
    }
}
